package com.eairlv.route.client.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.eairlv.route.client.configuration.RouteProperties;
import com.eairlv.route.client.entity.AuthenticationProperties;
import com.eairlv.route.client.entity.HttpResult;
import com.eairlv.route.client.entity.Result;
import com.eairlv.route.client.entity.RouteForm;
import com.eairlv.route.client.utils.LogUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author eairlv
 * @description
 * @date 9:55 2019/12/12
 */
@Slf4j
@Configuration
public class RouteClientService {

    @Getter
    private RouteProperties routeProperties;

    private RestTemplate routeTemplate;

    /**
     * 设置header
     */
    public RouteClientService(RouteProperties routeProperties, RestTemplate routeTemplate) {
        List<HttpMessageConverter<?>> converterList = routeTemplate.getMessageConverters();
        HttpMessageConverter<?> converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converterList.add(0, converter);
        this.routeProperties = routeProperties;
        this.routeTemplate = routeTemplate;
    }

    /**
     * 获取路由信息
     * @param key
     * @return
     */
    public AuthenticationProperties getRoute(String key, Integer type){
        try {
            String result = routeTemplate.getForObject("http://" + routeProperties.getService() + routeProperties.getUri() + "/" + key +
                    "?type=" + type, String.class);
            TypeReference<Result<AuthenticationProperties>> typeReference = new TypeReference<Result<AuthenticationProperties>>(){};
            Result<AuthenticationProperties> authenticationPropertiesResult = JSON.parseObject(result, typeReference);
            if (authenticationPropertiesResult != null && authenticationPropertiesResult.getData() != null){
                return authenticationPropertiesResult.getData();
            }
        } catch (Exception e){
            log.warn("get route authentication properties error: {}", LogUtil.getExceptionMessage(e));
        }
        return null;
    }

    /**
     * 获取路由信息
     * @param session
     * @return
     */
    public AuthenticationProperties getRouteBySession(String session){
        try {
            String result = routeTemplate.getForObject("http://" + routeProperties.getService() + routeProperties.getSessionUri() + "/" + session, String.class);
            TypeReference<Result<AuthenticationProperties>> typeReference = new TypeReference<Result<AuthenticationProperties>>(){};
            Result<AuthenticationProperties> authenticationPropertiesResult = JSON.parseObject(result, typeReference);
            if (authenticationPropertiesResult != null && authenticationPropertiesResult.getData() != null){
                return authenticationPropertiesResult.getData();
            }
        } catch (Exception e){
            log.warn("get route authentication properties error: {}", LogUtil.getExceptionMessage(e));
        }
        return null;
    }

    /**
     * 设置路由信息
     * @param routeForm
     * @return
     */
    public Boolean setRoute(RouteForm routeForm){
        try {
            Result result = routeTemplate.postForObject("http://" + routeProperties.getService() + routeProperties.getUri(),  routeForm, Result.class);
            return result != null && result.getCode() == HttpResult.SUCCESS.getCode();
        } catch (Exception e){
            log.warn("get route authentication properties error: {}", LogUtil.getExceptionMessage(e));
        }
        return Boolean.FALSE;
    }
}
